/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Package tracer for init trace provider
package tracer

import (
	"context"
	"errors"
	"testing"

	"github.com/agiledragon/gomonkey/v2"
	"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
	"go.opentelemetry.io/otel/sdk/resource"

	mockUtils "frontend/pkg/common/faas_common/utils"
)

func TestInitProvider(t *testing.T) {
	type args struct {
		ctx context.Context
	}
	otelGRPCEndpoint = "mockOtelGRPCEndpoint"
	otelGRPCToken = "mockOtelGRPCToken"
	otelServiceName = "mockOtelServiceName"
	enableOTELTracer = true
	tests := []struct {
		name        string
		args        args
		patchesFunc mockUtils.PatchesFunc
		wantErr     bool
	}{
		{
			name: "test success",
			args: args{
				ctx: context.Background(),
			},
			patchesFunc: func() mockUtils.PatchSlice {
				patches := mockUtils.InitPatchSlice()
				gomonkey.ApplyFunc(otlptrace.New, func(ctx context.Context,
					client otlptrace.Client) (*otlptrace.Exporter, error) {
					return &otlptrace.Exporter{}, nil
				})
				return patches
			},
			wantErr: false,
		}, // test success
		{
			name: "test error when new client",
			args: args{
				ctx: context.Background(),
			},
			patchesFunc: func() mockUtils.PatchSlice {
				patches := mockUtils.InitPatchSlice()
				gomonkey.ApplyFunc(otlptrace.New, func(ctx context.Context,
					client otlptrace.Client) (*otlptrace.Exporter, error) {
					return nil, errors.New("mock new client error")
				})
				return patches
			},
			wantErr: true,
		}, // test error when new client
		{
			name: "test success",
			args: args{
				ctx: context.Background(),
			},
			patchesFunc: func() mockUtils.PatchSlice {
				patches := mockUtils.InitPatchSlice()
				gomonkey.ApplyFunc(otlptrace.New, func(ctx context.Context,
					client otlptrace.Client) (*otlptrace.Exporter, error) {
					return &otlptrace.Exporter{}, nil
				})
				gomonkey.ApplyFunc(resource.New, func(ctx context.Context,
					opts ...resource.Option) (*resource.Resource, error) {
					return nil, errors.New("mock resource new error")
				})
				return patches
			},
			wantErr: true,
		}, // test success
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			patches := tt.patchesFunc()
			defer patches.ResetAll()
			_, err := InitProvider(tt.args.ctx)
			if (err != nil) != tt.wantErr {
				t.Errorf("InitProvider() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
		})
	}
}

func TestInitCommonTracer(t *testing.T) {
	type args struct {
		shutdown    func()
		serviceName string
	}
	isMocked := false
	tests := []struct {
		name        string
		args        args
		patchesFunc mockUtils.PatchesFunc
		isMocked    bool
	}{
		{
			name: "test with error",
			args: args{},
			patchesFunc: func() mockUtils.PatchSlice {
				patches := mockUtils.InitPatchSlice()
				gomonkey.ApplyFunc(InitProvider, func(ctx context.Context) (func(), error) {
					isMocked = true
					return nil, errors.New("mockInitProviderError")
				})
				return patches
			},
			isMocked: true,
		},
	}
	for _, tt := range tests {
		isMocked = false
		patches := tt.patchesFunc()
		defer patches.ResetAll()
		t.Run(tt.name, func(t *testing.T) {
			InitCommonTracer(tt.args.shutdown, tt.args.serviceName)
		})
		if tt.isMocked != isMocked {
			t.Errorf("expect %v but found %v", tt.isMocked, isMocked)
		}
	}
}
